<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="汇总人" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="汇总人"></el-input>
    </el-form-item>
    <el-form-item label="汇总的记录" prop="taskUserIds">
      <el-input v-model="dataForm.taskUserIds" placeholder="汇总的记录"></el-input>
    </el-form-item>
    <el-form-item label="数据来源" prop="dataSource">
      <el-input v-model="dataForm.dataSource" placeholder="数据来源"></el-input>
    </el-form-item>
    <el-form-item label="模板" prop="tempName">
      <el-input v-model="dataForm.tempName" placeholder="模板"></el-input>
    </el-form-item>
    <el-form-item label="任务名称" prop="taskName">
      <el-input v-model="dataForm.taskName" placeholder="任务名称"></el-input>
    </el-form-item>
    <el-form-item label="上报时间节点" prop="nodeDate">
      <el-input v-model="dataForm.nodeDate" placeholder="上报时间节点"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          taskUserIds: '',
          dataSource: '',
          tempName: '',
          taskName: '',
          nodeDate: '',
          createTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '汇总人不能为空', trigger: 'blur' }
          ],
          taskUserIds: [
            { required: true, message: '汇总的记录不能为空', trigger: 'blur' }
          ],
          dataSource: [
            { required: true, message: '数据来源不能为空', trigger: 'blur' }
          ],
          tempName: [
            { required: true, message: '模板不能为空', trigger: 'blur' }
          ],
          taskName: [
            { required: true, message: '任务名称不能为空', trigger: 'blur' }
          ],
          nodeDate: [
            { required: true, message: '上报时间节点不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/frt/sumary/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.sumary.userId
                this.dataForm.taskUserIds = data.sumary.taskUserIds
                this.dataForm.dataSource = data.sumary.dataSource
                this.dataForm.tempName = data.sumary.tempName
                this.dataForm.taskName = data.sumary.taskName
                this.dataForm.nodeDate = data.sumary.nodeDate
                this.dataForm.createTime = data.sumary.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/frt/sumary/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'taskUserIds': this.dataForm.taskUserIds,
                'dataSource': this.dataForm.dataSource,
                'tempName': this.dataForm.tempName,
                'taskName': this.dataForm.taskName,
                'nodeDate': this.dataForm.nodeDate,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
